package com.hz.wjy.controller;

import com.hz.wjy.service.UserService;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.jms.Queue;

@RestController
public class UserController {

    @Resource
    JmsMessagingTemplate jmsMessagingTemplate;

    @Resource
    Queue queue;

    @Resource
    UserService userService;

    @PostMapping("/user/add")
    public String addUser(@RequestParam("username") String username, @RequestParam("realname") String realname,
                          @RequestParam("password") String password, @RequestParam("schoolId") String schoolId,
                          @RequestParam("userType") int userType) {
        userService.addUser(username, password, realname, schoolId, userType);
        return "success";
    }


    @PostMapping("/user/login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password) {
        jmsMessagingTemplate.convertAndSend(queue, username + " try to login");
        return userService.login(username, password) ? "login sucess" : "error username or password";
    }

    @GetMapping("/user/islogin")
    public String isLogin(@RequestParam("username") String username) {
        return userService.isLogin(username) ? username + " has logined" : username +"not logined.";
    }

}
